#!/bin/bash
# $1 is the command that produces the list of file
# i.e. "ls *.eps"
# bash for loop

##################################
source ~/scriptbelt/bashutilities.sh
##################################

nospace=false

for iarg in $*; do
    option=`echo "$iarg" | cut -d"=" -f1`
    if [ "$option" == "-h"  ]; then
	echo 'example of use: loopydo  "ls *.eps" gv '
	echo "available options are:"
	echo "--nospace"
	exit
    fi

    if [ "$option" == "--nospace"  ]; then
	nospace=true
    fi

done 



SAVEIFS=$IFS

#IFS='\ '

if [ "$1" = "-h" ]; then
    echo 'example of use: loopydo  "ls *.eps" gv '
    exit
else 

    #    for f in $( $1 ); do
    $1 | while read f; do
	#	ps2pdf -dEPSCrop $f
        
	if [ "$nospace" = false ]; then
	    echo -e "executing: ""$2" "$f"
            $2 "$f"
	fi
	if [ "$nospace" = true ]; then
	    echo -e $RED"executing: ""$2""$f"$COL_RESET
	    $2"$f"
	    #echo $PATH
	fi
    done
fi

IFS=$SAVEIFS
